﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PathFinder
{
    public partial class NewProjectForm : Form
    {
        public NewProjectForm()
        {
            InitializeComponent(); 
            this.MinimumSize = this.Size;
            this.MaximumSize = this.MinimumSize;
        }

        private void BitmapBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Title = "Open Image File";
            dlg.Filter = "All image files (*.bmp;*.jpg;*.png;*.tif)|*.bmp;*.jpg;*.png;*.tif|" +
                "Bitmap Files (*.bmp)|*.bmp" +
                "|JPEG Files (*.jpg)|*.jpg" +
                "|PNG Files (*.png)|*.png|TIFF Files (*.tif)|*.tif";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                FilePathTextBox.Text = dlg.FileName;
            }
        }

        private void NoBackgroundRadio_CheckedChanged(object sender, EventArgs e)
        {
            if (NoBackgroundRadio.Checked)
            {
                FromFileRadio.Checked = false;
                WidthHeightPanel.Enabled = true;
                bitmapPanel.Enabled = false;
            }
            else
            {
                NoBackgroundRadio.Checked = false;
                bitmapPanel.Enabled = true;
                WidthHeightPanel.Enabled = false;
            }
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            Image img;
            if (FromFileRadio.Checked)
            {
                try
                {
                    img = Project.LoadImageSafelyFromFile(FilePathTextBox.Text);
                    if (img == null)
                    {
                        MessageBox.Show("Incorrect path or non-existing file!", "Warning");
                        return;
                    }
                        if (img.Width > 4000 || img.Height > 4000)
                        {
                            MessageBox.Show("Too big image, maximal size is 4000x4000!", "Warning");
                            return;
                        }
                    MainForm.instance.CreateNewProject(img);
                    this.Close();
                }
                catch (OutOfMemoryException exc)
                {
                    MessageBox.Show("Wrong file format!", "Warning");
                }
                catch (IOException exc)
                {
                    MessageBox.Show("Incorrect path or non-existing file!", "Warning");
                }
                catch (ArgumentException exc)
                {
                    MessageBox.Show("Unknown error!", "Warning");
                }

            }
            else
            {

                try
                {
                    int width = Int32.Parse(WidthTextBox.Text);
                    int height = Int32.Parse(HeightTextBox.Text);
                    if (width < 400 || height < 400)
                    {
                        MessageBox.Show("Minimal size is 400x400!", "Warning");
                    }
                    else if( width > 4000 || height > 4000)
                    {
                        MessageBox.Show("Maximal size is 4000x4000!", "Warning");
                    }
                    else
                    {
                        MainForm.instance.CreateNewProject(width, height);
                        this.Close();
                    }
                }
                catch (OverflowException exc)
                {
                    MessageBox.Show("Maximal size is 4000x4000!", "Warning");
                }
                catch (FormatException exc)
                {
                    MessageBox.Show("Width and height must be numbers!", "Warning");
                }
            }
        }

        public void Clear()
        {
            FilePathTextBox.Text = "";
            FromFileRadio.Checked = true;
            WidthTextBox.Text = "400";
            HeightTextBox.Text = "400";
        }
    }
}
